pub(crate) mod controller;

use std::future::Future;
use actix_web::{App, HttpServer, web, middleware::{self, Next, Logger}};
use actix_web::body::MessageBody;
use actix_web::dev::{forward_ready, Service, ServiceRequest, ServiceResponse};
use log::info;
use lipo_base::log_info;

pub(crate) async fn start_http_server() {
    unsafe { std::env::set_var("RUST_LOG", "info"); }
    unsafe { std::env::set_var("RUST_BACKTRACE", "1"); }
    env_logger::init();

    HttpServer::new(|| {
        App::new()
            .wrap(middleware::from_fn(my_mw))
            .wrap(Logger::default())
            .configure(controller::user_config)
        // .service(
        //     web::scope("/user")
        //         .service(inde)
        // )
    })
        .bind("127.0.0.1:8090").unwrap()
        .run().await.expect("构建失败");
}


/**
 * @Des: 我的中间拦截
 * @param:
 * @return:
 */
async fn my_mw(
    req: ServiceRequest,
    next: Next<impl MessageBody>,
) -> Result<ServiceResponse<impl MessageBody>, actix_web::Error> {
    // pre-processing
    let header_map = req.headers();

    if !header_map.contains_key("token") {
      return Err(actix_web::error::ErrorUnauthorized("缺少Token信息"));
    }
    // invoke the wrapped middleware or service
    let res = next.call(req).await?;

    Ok(res)
}

